/*!
 * tykhome - tag.js
 * Author tykhome <yongketian@gmail.com>
 */

var models = require('../models'),
	Tag = models.Tag;

exports.getTagByName = function (name, callback) {
	Tag.findOne({
		name: name
	}, callback);
};

/**
 * 根据标签ID列表，获取一组标签
 * Callback:
 * - err, 数据库异常
 * - tags, 标签列表
 * @param {Array} ids 标签ID列表
 * @param {Function} callback 回调函数
 */
exports.getTagsByIds = function (ids, callback) {
	Tag.find({
		_id: {
			'$in': ids
		}
	}, callback);
};

/**
 * 获取所有标签
 * Callback:
 * - err, 数据库异常
 * - tags, 标签列表
 * @param {Function} callback 回调函数
 */
exports.getAllTags = function (callback) {
	Tag.find({}, [], {
		sort: [['order', 'asc']]
	}, callback);
};


exports.getTagsByQuery = function (query, fields, opt, callback) {
	Tag.find(query, fields, opt, callback);
};

/**
 * 根据标签ID获取标签
 * Callback:
 * - err, 数据库异常
 * - tag, 标签
 * @param {String} id 标签ID
 * @param {Function} callback 回调函数
 */
exports.getTagById = function (id, callback) {
	Tag.findOne({
		_id: id
	}, callback);
};

exports.updateCount = function (tag_name, callback) {
	Tag.findOneAndUpdate({
		name: tag_name
	}, {
		$set: {
			name: tag_name
		},
		$inc: {
			soft_count: 1
		}
	}, {
		upsert: true
	}, callback);
};

exports.newAndSave = function (name, background, order, description, callback) {
	var tag = new Tag();
	tag.name = name;
	tag.background = background;
	tag.order = order;
	tag.description = description;
	tag.save(callback);
};